import json

from django.shortcuts import render
from django.shortcuts import redirect
from django.shortcuts import reverse
from django.shortcuts import HttpResponse
from django.http import JsonResponse

import models


# Create your views here.
def index(request):
    return HttpResponse("OK")


# {"param":{"skuld":123,"num":10}}
def place(request):
    """
    下单接口
    :param request:
    :return:
    """
    if request.method == "POST":
        request_params = json.loads(request.body)
        param = request_params.get("param")
        if param:
            kid = param.get("skuld")
            num = param.get("num")
            if kid and num:
                return JsonResponse({"code": "0", "message": "success"})
        return JsonResponse({"code": 1})
    return JsonResponse({"code": 1, "message": "非法请求"})


def repertory(request):
    """
    查询库存
    :param request:
    :return:
    """
    if request.method == "GET":
        rid = request.GET.get("id")
        if rid:
            return JsonResponse({"code": "0", "message": "success"})
        return JsonResponse({"code": 1})
    return JsonResponse({"code": 1, "message": "非法请求"})


def login(request):
    """
    登录
    :param request:
    :return:
    """
    if request.method == "POST":
        username = request.POST.get("userName")
        password = request.POST.get("password")
        if username == "admin" and password == "1234":
            # redirect("set_cookies")
            request.session['admin'] = "login"
            request.session.set_expiry(60 * 60)
            return JsonResponse({"code": "0", "message": "success"})
    return JsonResponse({"code": 1, "message": "非法请求"})


def login2(request):
    """
    token登录
    :param request:
    :return:
    """
    if request.method == "POST":
        user_name = request.POST.get("userName")
        password = request.POST.get("password")
        if user_name and password:
            return JsonResponse({"code": "0", "message": "success", "data": "admin22"})
    return JsonResponse({"code": 1, "message": "非法请求"})


def login3(request):
    """
    普通登录
    :param request:
    :return:
    """
    if request.method == "POST":
        user_name = request.POST.get("userName")
        password = request.POST.get("password")
        if user_name and password:
            return JsonResponse({"code": "0", "message": "success", "data": "admin22"})


def query(request):
    """查询余额"""
    print(request.COOKIES)
    if request.method == "GET":
        username = request.GET.get("userName")
        cookies_username = request.COOKIES
        if username and cookies_username:
            return JsonResponse({"code": "0", "message": "success"})
    return JsonResponse({"code": 1, "message": "非法请求"})


def query2(request):
    """
    携带请求头
    :param request:
    :return:
    """
    if request.method == "GET":
        user_name = request.GET.get("userName")
        header_username = request.META.get("HTTP_USERNAME")
        header_token = request.META.get("HTTP_TESTFAN_TOKEN")
        if user_name and header_username and header_token:
            return JsonResponse({"code": "0", "message": "success"})
    return JsonResponse({"code": "1", "message": "非法请求"})
